package com.iris.smartclass.controller;

import com.iris.smartclass.common.BaseResponse;
import com.iris.smartclass.common.ResultUtils;
import com.iris.smartclass.model.dto.assignment.AssignmentAddRequest;
import com.iris.smartclass.model.entity.Assignment;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/assignment")
public class HomeWorkController {

    @Resource
    private RedisTemplate<String, Assignment> redisTemplate;

    private static final String KEY = "Assignment";

    @PostMapping("/add")
    public BaseResponse<Boolean> createAssignment(@RequestBody AssignmentAddRequest assignmentAddRequest) {
        Assignment assignment = new Assignment();
        BeanUtils.copyProperties(assignmentAddRequest, assignment);
        redisTemplate.opsForList().rightPush(KEY, assignment);
        redisTemplate.expire(KEY,1, TimeUnit.DAYS);
        return ResultUtils.success(true);
    }


    @GetMapping("/getAllAssignment")
    public BaseResponse<List<Assignment>> getAllAssignment(){
        List<Assignment> assignmentList = redisTemplate.opsForList().range(KEY, 0, -1);
        return ResultUtils.success(assignmentList);
    }


}
